/** [getLocation 获取当前位置，默认使用百度地图定位] */

function getLocation(callback, type) {
	var popup = 1;
	if(!isPhone) {
		callback && callback.call(null, {
			formatted_address: "北京市昌平区霍营国风美唐2号楼",
			location: {
				"lat": 40.072861,
				"lng": 116.360278
			},
			addressComponent: {
				"province": "北京市",
				"street_number": "国风美唐2号楼",
				"district": "昌平区",
				"street": "霍营",
				"city": "北京市"
			}
		});
		return;
	}
	uexLocation.openLocation(type || null, function(err) {
		if(isIOS) {
			if(err == 1) {
				confirm2(function(data) {
					if(data == 0) {
						var params = {
							setting: 'GPS'
						};
						var datas = JSON.stringify(params);
						uexDevice.openSetting(datas);
					}
				}, "未开启定位服务，请开启定位服务", '允许智农云定位服务', ['设置', '取消']);
			} else {

			}
		} else {
			if(!err) {

			} else {
				confirm2(function(data) {
					if(data == 0) {
						var params = {
							setting: ''
						};
						var datas = JSON.stringify(params);
						uexDevice.openSetting(datas);
					}
				}, '未开启定位服务，请开启定位服务', '允许智农云定位服务', ['设置', '取消']);
			}
		}
	});

	uexLocation.onChange = function(lat, lng) {
		if(!lat || !lng) {
			if(popup) {
				confirm2(function(data) {
					if(data == 0) {
						var params = {
							setting: ""
						};
						var datas = JSON.stringify(params);
						uexDevice.openSetting(datas);
					} else {
						uexLocation.closeLocation();
					}
				}, '未开启定位服务，请开启定位服务', '允许智农云定位服务', ['设置', '取消']);
				popup = 0;
			} else {
				uexLocation.closeLocation();
			}
			return;
		}
		uexLocation.getAddressByType(JSON.stringify({
			latitude: lat,
			longitude: lng,
			// type: 'gcj02', // "wgs84":采用世界标准经纬度坐标;"bd09":采用百度地图的经纬度坐标;"gcj02":采用高德地图的经纬度坐标
			flag: 1
		}), function(err, data) {
			if(!err) {
				uexLocation.closeLocation();
				if(isAndroid) {
					var bd_lat = data.location.lat,
						bd_lon = data.location.lng;
					//百度地图坐标转成高德地图坐标
					var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
					var x = bd_lon - 0.0065,
						y = bd_lat - 0.006;
					var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
					var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
					data.location.lng = z * Math.cos(theta);
					data.location.lat = z * Math.sin(theta);
				}
				callback && callback.call(null, data);
			} else {
				callback && callback.call(null, {
					addressComponent: {
						city: '北京市'
					}
				});
				// alert('获取位置失败');
			}
		});
	};
}